{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "if os.environ.get('TEST_ARCHCTA_OFF', False):\n",
    "    log.debug(\"TEST_ARCHCTA_OFF enabled, skipping test\") \n",
    "\n",
    "import pydicom\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "from bmutils.insighttoolkit.utils import npy_to_png_str\n",
    "from bmutils.utils import npy2str\n",
    "from bmutils.series import generate_series_dicom\n",
    "from bmutils.diseases import Diseases\n",
    "from bmutils.pipelines import Pipeline, Transform, AIEnginePredict, AIEngineTemplateToReport, \\\n",
    "    ProtocolValidator, BiomindOverviewValidator, TemplateToReportValidator, AnnotationValidator, \\\n",
    "    GenerateTestImage, AnyToNumpy, Rescale2d\n",
    "\n",
    "archcta_case1_path = \"/deps/Biomind-Test-Data/data/NECK_CTA001\"\n",
    "archcta_case1_info = pydicom.read_file(os.path.join(archcta_case1_path, \"022bf07e-5de9c15f-2c0649c3-3d08bc0f-af4af5c9\"))\n",
    "\n",
    "# test case\n",
    "test_case = {\n",
    "    \"dcm_path\": archcta_case1_path,\n",
    "    \"study_uid\": archcta_case1_info.StudyInstanceUID,\n",
    "    \"protocols\": {\n",
    "        'pseries_classifier': {\n",
    "            'ARCHCTA': archcta_case1_info.SeriesInstanceUID,\n",
    "        },\n",
    "        'penable_cached_results': False\n",
    "    }\n",
    "}\n",
    "\n",
    "# additional tests\n",
    "def test_additional(test_case, payload):\n",
    "    assert test_case[\"study_uid\"] == payload['pstudy_uid']\n",
    "    return True\n",
    "\n",
    "aiengine = AIEnginePredict(host=\"https://192.168.2.44\")\n",
    "\n",
    "payload = Pipeline.pipe(\n",
    "    aiengine,\n",
    "    # validate protocols\n",
    "    ProtocolValidator()\n",
    ").split(\n",
    "    # validate model specific tests\n",
    "    Transform(lambda x: test_additional(test_case, x))\n",
    ")([test_case[\"study_uid\"], test_case[\"protocols\"]])\n",
    "\n",
    "series = generate_series_dicom(test_case['dcm_path'])\n",
    "print(\"payload:\",payload)\n",
    "series_uid = list(payload[0][\"pprediction\"].keys())\n",
    "mask = payload[0][\"pprediction\"][series_uid[0]][0]['mask']\n",
    "\n",
    "# create dummy anotation\n",
    "annotation = {\n",
    "    'pannotation': {\n",
    "        archcta_case1_info.SeriesInstanceUID: {\n",
    "            Diseases.stenosis.key: {\n",
    "                'type': 'SegmentationMasksNpy',\n",
    "                'annotation': [mask]\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "}   \n",
    "\n",
    "# validate annotation\n",
    "if os.environ.get('TEST_ANNOTATION_OFF', False):\n",
    "    log.debug(\"TEST_ANNOTATION_OFF enabled, skipping test\")\n",
    "else:\n",
    "    # validate annotation\n",
    "    AnnotationValidator(aiengine, series)([\n",
    "        test_case[\"study_uid\"],\n",
    "        { **test_case[\"protocols\"], **annotation}\n",
    "    ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
